package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.CourseDO;
import com.qk.dto.course.CourseDTO;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-10-11 21:04
 */
@RestController
@RequestMapping("/courses")
public class CourseController {
    private static final String SUBJECT = "subject";
    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result listPage(CourseDTO courseDTO) {
        PageResult<CourseDO> pageResultCourseDO = courseService.listPage(courseDTO);
        return Result.success(pageResultCourseDO);
    }
    @LogAnno
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        courseService.removeById(id);
        return Result.success();
    }
    @LogAnno
    @PostMapping
    public Result save(@RequestBody CourseDO courseDO) {
        courseDO.setCreateTime(LocalDateTime.now());
        courseDO.setUpdateTime(LocalDateTime.now());
        courseService.save(courseDO);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        CourseDO courseDO = courseService.getById(id);
        return Result.success(courseDO);
    }
    @LogAnno
    @PutMapping
    public Result update(@RequestBody CourseDO courseDO) {
        courseDO.setUpdateTime(LocalDateTime.now());
        courseService.updateById(courseDO);
        return Result.success();
    }

    @GetMapping("/list")
    public Result listAll() {
        List<CourseDO> list = courseService.list();
        return Result.success(list);
    }

    @GetMapping("/subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject) {
        List<CourseDO> list = courseService.listByMap(Map.of(SUBJECT, subject));
        return Result.success(list);
    }
}
